/*
 * This file is part of the Sx Framework Library.
 * 
 * Copyright (C) 2013 University of Colorado Denver
 * <min.choi@ucdenver.edu> <shane.transue@ucdenver.edu>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */
#ifndef SX_CAMERA_H
#define SX_CAMERA_H

#include <sxMath.h>
#include <sxGraphicsObject.h>
#include <iostream>

namespace Sx {
namespace Graphics {
namespace Objects {

using Math::Projection;
using Math::Perspective;
using Math::View;

/*
 * This class provides a virtual camera capabile of displaying  a virtual
 * scene based on various projections, perspectives, and views. This Camera
 * class has been designed for use with a two dimensional screen interface.
 * Based on the movements of a cursor, this camera can be manipulated in a
 * variety of ways.
 */
class Camera : public GraphicsObject {
public:
	Camera();
	Camera(Projection projection, Perspective perspective);
	Camera(Projection projection, Perspective perspective, unsigned int width, unsigned int height);
	Camera(Projection projection, Perspective perspective, float fov, float nearPlane, float farPlane, int width, int height);
	virtual ~Camera();

	bool construct();
	void update(float dt);

	void setSize(unsigned int width, unsigned int height);
	void resize(unsigned int width, unsigned int height);

	/*
	 * Rotate the camera. Based on the perspective and projection of this
	 * Camera, this function will behave according to these parameters.
	 */
	void rotate(int xDifference, int yDifference);
	void scope(int xDifference, int yDifference);
	void pan(int xDifference, int yDifference);
	void zoom(float amount);
	void walk(int xDifference, int yDifference);
	
	void setAspectRatio(float ratio);
	void setVRadians(float v);
	void setHRadians(float h);
	void rotate(float hRads, float vRads);
	void resetRotation();
	void setPosition(float x, float y, float z);
	void setPosition(const Eigen::Vector3f& position);
	void setProjection(Projection projection);
	void setPerspective(Perspective perspective);
	void invertX(bool b);
	void invertY(bool b);
	void setView(View view);
	View getView() const;

	int getWidth() const;
	int getHeight() const;

	/* 
	 * Limits the freedom of the camera to the coordinate space defined between
	 * the minimum and maximum values.
	 */
	void setLimitOrtho(bool b);
	void setOrthoMin(const Eigen::Vector3f& min);
	void setOrthoMax(const Eigen::Vector3f& max);
	void setOrthoZoomSensitivity(float sensitivity);

	float getOrthoLeft() const;
	float getOrthoRight() const;
	float getOrthoTop() const;
	float getOrthoBottom() const;
	float getOrthoWidth() const;
	float getOrthoHeight() const;
	float getOrthoZoom() const;
	float getRadius() const;
	float getAspect() const;

	void setEye(Eigen::Vector3f eye);
	void setLookAt(Eigen::Vector3f lookAt);
	void setUp(Eigen::Vector3f up);

	Eigen::Vector3f getEye() const;
	Eigen::Vector3f getLookAt() const;
	Eigen::Vector3f getUp() const;

	Eigen::Vector3f getViewDirection() const;
	Eigen::Vector3f getRightDirection() const;

	void setMoveSpeed(float speed);
	void setSensitivityX(float sensitivity);
	void setSensitivityY(float sensitivity);
	void setMinimumRadius(float radius);
	void setMaximumRadius(float radius);
	void setFarPlane(float distance);
	void setNearPlane(float distance);
	void setFieldOfView(float angle);
	void addRadius(float value);

	float getMinimumRadius() const;
	float getMaximumRadius() const;
	float getFarPlane() const;
	float getNearPlane() const;
	float getFOV() const;
	float getConX() const;
	float getConY() const;
	float getPanSpeed() const;
	Eigen::Vector3f getDirection() const;

	Perspective getPerspective() const;
	Projection getProjection() const;
	
protected:
	float hRadians, vRadians;

	Perspective perspective;
	Projection projection;
	View view;

	void initialize();
	void rotate_first_person();
	void rotate_third_person();
	void zoom_orthographic(float amount);
	void zoom_third_person(float amount);
	void pan_third_person(float xAmount, float yAmount);
	void pan_first_person(float xAmount, float yAmount);
	void pan_orthographic(int xDifference, int yDifference);
	void strafe_first_person(float amount);
	void elevate_first_person(float amount);
	void pan_horizontal_clamp(float distance);
	void rotate();
	float calculate_ortho_zoom(float amount) const;

	Math::Vector3f eye, lookAt, up;

	bool visible;
	bool invertedX, invertedY;
	float panSpeed;
	float radiusPanFactor;
	float moveSpeed;

	float sensitivityX, sensitivityY;
	float radius;
	float minimumRadius, maximumRadius;
	

	unsigned int width, height;
	float aspectRatio;
	float nearPlane, farPlane;
	Math::Vector3f orthoMin, orthoMax;
	bool limitOrtho;
	float orthoZoomSensitivity;
	float orthoWidth, orthoHeight, orthoZoom;
	float conX, conY;
	float fov;
};

}

}

}

#endif
